#pragma once

#include "../../kernel/KernelHead.h"

#pragma pack(1)
struct TagNCMsg
{
	int			size;
	short		cmd;
};

struct TagNSMsg
{
	int			size;
	short		cmd;
};
#pragma pack()

class SocketMgr
{
public:
	
	static SocketMgr* inst()
	{
		static SocketMgr* instance = new SocketMgr();
		return instance;
	}

	~SocketMgr(void);

	bool initSocket(std::string ip, short adport);

	template<typename t_class, typename t_fun>
	bool addEventListener( int cmd, std::string key, t_class *obj, t_fun objFun )
	{
		bool b = _eventContainer.addEventListener(key, obj, objFun);

		if (b) _keyList.insert(make_pair(cmd, key));

		return b;
	}

	template<typename t_class, typename t_fun>
	bool removeEventListener( int cmd, std::string strKey, t_class *obj, t_fun objFun)
	{
		bool b = _eventContainer.removeEventListener(strKey, obj, objFun);
		if(b)
		{
			auto it = _keyList.find(int);
			if (it != _keyList.end()) _keyList.erase(it);
		}
		return b;
	}


	void sendMsg(TagNCMsg* data);
	void recvMsg(void* data);
private:

	SocketMgr(void);

private:
	ClientSocket						_clientSocket;
	EventContainer					_eventContainer;
	map<int, string>				_keyList;
};

